package com.jing.taobao.controller.spread;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import weibo4j.model.Status;
import weibo4j.model.WeiboException;

import com.jing.system.comm.BaseController;
import com.jing.system.comm.contants.SystemCons;
import com.jing.system.comm.contants.WeiboCons;
import com.jing.system.utils.StringUtil;
import com.jing.taobao.pojo.spread.TbSpreadType;
import com.jing.taobao.pojo.spread.TbSpreadinfo;
import com.jing.taobao.pojo.user.TaobaoUser;
import com.jing.taobao.service.spread.TbSpreadTypeService;
import com.jing.taobao.service.spread.TbSpreadinfoService;
import com.jing.weibo.pojo.user.UserInfo;
import com.jing.weibo.ws.sina.TimelineService;

/**
 * 淘宝推广微博的Controller
 * @author jing.yue
 * @version 2012/08/29 1.0.0
 */
@Controller
public class TbSpreadinfoController extends BaseController {

	private static final Logger logger = Logger.getLogger(TbSpreadinfoController.class);

	@Autowired
	private TimelineService timelineService;
	@Autowired
	private TbSpreadTypeService tbSpreadTypeService;
	@Autowired
	private TbSpreadinfoService tbSpreadinfoService;

	/**
	 * 跳转到添加推广微博的页面
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/toAddTbSpreadinfo")
	public String toAddTbSpreadinfo(HttpServletRequest request, ModelMap modelMap) {
		TaobaoUser taobaoUser = getSessionTaobaoUser(request);
		try {
			List<TbSpreadType> tbSpreadTypes = tbSpreadTypeService.findAllParentTbSpreadType();
			modelMap.put("tbSpreadTypes", tbSpreadTypes);
		} catch (Exception e) {
			logger.error(taobaoUser.getNickname() + " :" + e.getMessage(), e);
			return ERROR;
		}
		return "taobao/user/spreadinfo/spreadinfo_add";
	}

	/**
	 * 添加推广信息
	 * @return
	 */
	@RequestMapping(value = "/tbSpreadinfo/json/addTbSpreadinfo")
	@ResponseBody
	public ModelMap addTbSpreadinfo(HttpServletRequest request, TbSpreadinfo tbSpreadinfo) {
		String result = null;
		StringBuffer errorWeiboMsg = new StringBuffer();
		UserInfo userInfo = getSessionUser(request);
		if(userInfo == null) {
			result = ERROR_NOT_LOGIN;
		} else {
			TaobaoUser taobaoUser = getSessionTaobaoUser(request);
			try {

				Status status = null;
				if(StringUtil.isNotEmpty(tbSpreadinfo.getOriginalurl())) {
					StringBuffer imgUrl = new StringBuffer(SystemCons.ABSOLUTE_PATH);
					imgUrl.append(File.separator).append(SystemCons.UPLOAD_IMAGE_PATH).append(tbSpreadinfo.getOriginalurl());
					//发送到微博
					status = timelineService.updateStatus(tbSpreadinfo.getContent().trim(), imgUrl.toString(), userInfo.getToken());
				}
				else {
					//发送到微博
					status = timelineService.updateStatus(tbSpreadinfo.getContent().trim(), userInfo.getToken());
				}
				//设置图片
				tbSpreadinfo.setOriginalurl(status.getOriginalPic());
				tbSpreadinfo.setThumbnailurl(status.getThumbnailPic());
				//设置添加的淘宝用户名
				tbSpreadinfo.setTaobaousername(taobaoUser.getUsername());
				//设置添加的微博用户名
				tbSpreadinfo.setWeibousername(userInfo.getUsername());
				//设置微博ID
				tbSpreadinfo.setWeiboid(status.getId());

				tbSpreadinfoService.saveTbSpreadinfo(tbSpreadinfo);

				result = SUCCESS;
			} catch (WeiboException e) {
				logger.error(WeiboCons.getErrorMsg(e));
				result = ERROR_WEIBO;
				errorWeiboMsg.append("调用微博接口出现异常[").append(WeiboCons.getErrorMsg(e)).append("]!");
			} catch (Exception e) {
				result = ERROR;
				logger.error(userInfo.getNickname() + ": 添加推广信息异常!", e);
			}
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("result", result);
		modelMap.put("errorWeiboMsg", errorWeiboMsg.toString());
		return modelMap;
	}
}